<template>
  <div class="loginbody">
    <div class="logindata">
      <div class="logintext">
        <h1>校园活动平台</h1>
        <h2>请选择登录身份</h2>
      </div>

      <!-- 身份选择 -->
      <div class="role-select">
        <el-radio-group v-model="loginRole" size="large">
          <el-radio-button label="student">学生端登录</el-radio-button>
          <el-radio-button label="school">学校端登录</el-radio-button>
        </el-radio-group>
      </div>

      <div class="formdata">
        <el-form ref="form">
          <el-form-item prop="account">
            <el-input v-model="loginForm.account" clearable
              :placeholder="loginRole === 'student' ? '请输入学生账号' : '请输入学校账号'"></el-input>
          </el-form-item>
          <el-form-item prop="password">
            <el-input v-model="loginForm.password" clearable placeholder="请输入密码" show-password></el-input>
          </el-form-item>
        </el-form>
      </div>
      <div class="butt">
        <el-button @click="handleLogin" :loading="buttonLoading" type="primary">
          {{ loginRole === 'student' ? '学生登录' : '学校登录' }}
        </el-button>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref } from "vue"
import { useRouter } from "vue-router"
import { ElMessage, ElMessageBox } from "element-plus"
import { login as studentLogin } from "@/api/student"
import { login as schoolLogin } from "@/api/school" // 需要新建学校端API

const router = useRouter()

const loginRole = ref('student') // 默认学生端
const loginForm = ref({
  account: "stu001",
  password: "123456",
})

const buttonLoading = ref(false)

const handleLogin = () => {
  buttonLoading.value = true

  if (loginRole.value === 'student') {
    // 学生登录
    studentLogin(loginForm.value).then((res) => {
      handleLoginSuccess(res, 'student')
    }).catch((error) => {
      ElMessage.error(error?.msg || "登录失败")
    }).finally(() => {
      buttonLoading.value = false
    })
  } else {
    // 学校登录
    schoolLogin(loginForm.value).then((res) => {
      handleLoginSuccess(res, 'school')
    }).catch((error) => {
      ElMessage.error(error?.msg || "登录失败")
    }).finally(() => {
      buttonLoading.value = false
    })
  }
}

const handleLoginSuccess = (res, role) => {
  const info = res.data

  if (!info || !info.token) {
    ElMessage.error("登录失败，请检查用户名和密码")
    return
  }

  // 存储登录信息
  localStorage.setItem("token", info.token)
  localStorage.setItem("userRole", role)
  localStorage.setItem("userInfo", JSON.stringify({
    id: info.id,
    name: info.name,
    account: info.account,
    avatar: info.avatar,
    role: role
  }))

  ElMessage.success("登录成功")

  // 根据身份跳转到不同页面
  if (role === 'student') {
    router.push({ path: "/student/activity" })
  } else {
    router.push({ path: "/school/dashboard" }) // 学校端首页
  }
}

</script>

<style scoped>
.role-select {
  margin-bottom: 20px;
  text-align: center;
}

.loginbody {
  width: 100%;
  height: 100%;
  min-width: 1000px;
  background-image: url("@/assets/background.jpg");
  background-size: 100% 100%;
  background-position: center center;
  overflow: auto;
  background-repeat: no-repeat;
  position: fixed;
  line-height: 100%;
  padding-top: 150px;
}

.logintext {
  margin-bottom: 20px;
  line-height: 50px;
  text-align: center;
  font-size: 30px;
  font-weight: bolder;
  color: white;
  text-shadow: 2px 2px 4px #000000;
}

.logindata {
  width: 400px;
  height: 300px;
  transform: translate(-50%);
  margin-left: 50%;
}

.tool {
  display: flex;
  justify-content: space-between;
  color: #606266;
}

.butt {
  margin-top: 10px;
  text-align: center;
}

.shou {
  cursor: pointer;
  color: #606266;
}

/*ui*/
/* /deep/ .el-form-item__label {
  font-weight: bolder;
  font-size: 15px;
  text-align: left;
}

/deep/ .el-button {
  width: 100%;
  margin-bottom: 10px;

} */
</style>